package cn.edu.sjtu.se.dslab.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.sjtu.se.dslab.component.method.MethodComponent;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.project.ProjectMethod;
import cn.edu.sjtu.se.dslab.service.IMethodComponentService;
import cn.edu.sjtu.se.dslab.service.IProjectMethodService;
import cn.edu.sjtu.se.dslab.service.IProjectService;
import cn.edu.sjtu.se.dslab.util.Tuple;
import cn.edu.sjtu.se.dslab.util.TwoTuple;

@Controller
@RequestMapping(value = "/projectMethod")
public class ProjectMethodController {
	private static final Logger logger = Logger
			.getLogger(ProjectController.class);

	@Resource(name = "projectService")
	IProjectService projectService;

	@Resource(name = "projectMethodService")
	IProjectMethodService projectMethodService;
	
	@Resource(name = "methodComponentService")
	IMethodComponentService methodComponentService;

	@RequestMapping(value = "{projectId}/add", method = RequestMethod.POST)
	public String addProject(@PathVariable("projectId") Long projectId,
			@RequestParam Long[] methodComponentIds) {
		Project project = projectService.getProjectById(projectId);
		if (project== null ) throw new NullPointerException("No such Project, id =" + projectId);
		projectMethodService.addProjectMethods(projectId, methodComponentIds);
		return "redirect:/projectMethod/" + projectId + "/list";
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public String deleteProjectMethod(@RequestParam Long[] projectMethodId, @RequestParam Long projectId){
		projectMethodService.deleteProjectMethodByIds(projectMethodId);
		return "redirect:/project/list/" + projectId + "/user";
	}
	
	@RequestMapping(value = "{projectId}/list", method = RequestMethod.GET)
	public ModelAndView getProjectMethodList(@PathVariable("projectId") Long projectId) {
		ModelAndView mav = new ModelAndView();
		Project project = projectService.getProjectById(projectId);
		if (null == project) throw new NullPointerException("No such project exist , id=" + projectId);
		mav.setViewName("downLoadCode");

		//已有的methodComponent和project
		List<ProjectMethod> projectMethods = projectMethodService.getListByProject(project);
		Map<Long, ProjectMethod> projectMethodMapping = new HashMap<Long, ProjectMethod>();
		for(ProjectMethod pm: projectMethods){
			projectMethodMapping.put(pm.getMethodComponent().getId(), pm);
			logger.info("Exist Method Component =" + pm.getMethodComponent().getName());
		}
		
		
		//所有的methodcomponent
		List<TwoTuple<MethodComponent, ProjectMethod>> projectMethodTuples = 
				new ArrayList<TwoTuple<MethodComponent, ProjectMethod>>();
		List<MethodComponent> methodComponents = methodComponentService.getAllMethodComponents();
		ProjectMethod projectMethod = new ProjectMethod();
		for (MethodComponent mc : methodComponents){
			logger.info("All method Component = " + mc.getName());
			if (projectMethodMapping.containsKey(mc.getId()))
				projectMethodTuples.add(Tuple.tuple(mc, projectMethodMapping.get(mc.getId())));
			else
				projectMethodTuples.add(Tuple.tuple(mc, projectMethod));
		}
		mav.addObject("projectMethodTuples", projectMethodTuples);
		mav.addObject("project", project);
		return mav;
	}

}
